/**
 * Copyright (C) 2015. Keegan小钢（http://keeganlee.me）
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.wengwyue.patientapplication.network;

import android.util.Log;
import android.widget.Toast;

import com.example.wengwyue.patientapplication.base.PatientApplication;
import com.example.wengwyue.patientapplication.model.param.LoginParam;
import com.example.wengwyue.patientapplication.model.param.PayParam;
import com.example.wengwyue.patientapplication.model.result.CaseBaseInfoResult;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hospital.common.api.CallbackUtils;
import com.hospital.common.api.DataCallback;
import com.hospital.common.api.model.BaseModel;
import com.hospital.common.api.model.Error;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * 作者： wenyue   时间： 16/6/4.
 * 功能： 网络请求管理类
 * 修改：
 */
public class ApiManager {

    /**
     * 请求病患信息接口
     * @param callback
     */
    public static void getCaseInfo(final DataCallback callback) {
        CallbackUtils.get(API.GET_SICK_CASE, new DataCallback() {
            @Override
            public void onSuccess(Object data) {
                CaseBaseInfoResult result = new Gson().fromJson(data.toString(), new TypeToken<CaseBaseInfoResult>() {
                }.getType());
                callback.onSuccess(result);
            }

            @Override
            public void onFailed(Error error) {
                callback.onFailed(error);
            }

        });
    }
    public static void putBalance(double money, final DataCallback callback) {
        CallbackUtils.put(API.PUT_BALANCE, new Gson().toJson(new PayParam(money)), new DataCallback() {
            @Override
            public void onSuccess(Object data) {
                callback.onSuccess(data);
            }

            @Override
            public void onFailed(Error error) {
                callback.onFailed(error);
            }

        });
    }

    public static void login(String data, final DataCallback callback) {
        CallbackUtils.post(API.POST_LOGIN, new Gson().toJson(new LoginParam(data)), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

                Log.d("networkerror", "dologin:onerror----------------->");

            }

            @Override
            public void onResponse(String response, int id) {
                BaseModel data = new Gson().fromJson(response, BaseModel.class);
                if (data.error != null && data.error.hasError) {
                    callback.onFailed(data.error);
                } else {
                    callback.onSuccess(data.error);
                }
            }
        });
    }
}
